//
//  MainViewController.swift
//  weibo
//
//  Created by apple on 16/4/7.
//  Copyright © 2016年 wangjianwei. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    // MARK: - 监听方法
    // @objc能够保证运行循环能发送此消息
    @objc private func clickComposedButton() {
        
        print("点我了")
    
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()

        setupComposedButton()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composedButton)
    }
    
    // MARK: - 懒加载控件
    private lazy var composedButton: UIButton = UIButton(
        
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")!
}
// MARK: - 设置界面
extension MainViewController {
    // 添加撰写按钮
    private func setupComposedButton() {
        
        // 1. 添加按钮
        tabBar.addSubview(composedButton)
        
        // 2. 调整按钮
        let count = childViewControllers.count
        
        let w = tabBar.bounds.width / CGFloat(count) - 1
        
        composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
        // 3. 添加点击方法
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: .TouchUpInside)
    
    }
    
    /// 添加控制器
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }

    /// 添加所有控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName:"tabbar_profile" )
    
    }
    
    
}
